﻿using System;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;

namespace NamazVakti
{
    public class XmlUtil
    {
        public static void Serialize(object obj)
        {
            TextWriter tw = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                tw = new StreamWriter(Parameters.FilePath);
                serializer.Serialize(tw, obj);
            }
            catch { }
            finally
            {
                if (tw != null)
                    tw.Close();
            }
        }

        public static object Deserialize(Type type)
        {
            TextReader reader = null;
            Object obj = null;
            try
            {
                if (!File.Exists(Parameters.FilePath)) return null;

                XmlSerializer serializer = new XmlSerializer(type);
                reader = new StreamReader(Parameters.FilePath);
                obj = serializer.Deserialize(reader);
            }
            catch { }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            reader.Close();
            return obj;
        }

    }
}
